package com.weaving.monitor.service;

import com.weaving.monitor.dto.PendingPlanDto;
import com.weaving.monitor.entity.PendingPlan;
import com.weaving.monitor.repository.PendingPlanRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PendingPlanService {
    
    private final PendingPlanRepository pendingPlanRepository;
    
    public PendingPlanDto getAllPendingPlans() {
        List<Integer> machineNumbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);
        List<PendingPlan> plans = pendingPlanRepository.findByMachineNumberInOrderByRoundNumberAsc(machineNumbers);
        
        Map<Integer, Map<Integer, String>> plansMap = new HashMap<>();
        
        // 初始化所有机器的轮次计划
        for (Integer machineNumber : machineNumbers) {
            Map<Integer, String> roundPlans = new HashMap<>();
            roundPlans.put(1, "");
            roundPlans.put(2, "");
            roundPlans.put(3, "");
            plansMap.put(machineNumber, roundPlans);
        }
        
        // 填充实际数据
        for (PendingPlan plan : plans) {
            Map<Integer, String> roundPlans = plansMap.get(plan.getMachineNumber());
            if (roundPlans != null) {
                roundPlans.put(plan.getRoundNumber(), plan.getPlanDetails());
            }
        }
        
        PendingPlanDto dto = new PendingPlanDto();
        dto.setPlans(plansMap);
        return dto;
    }
} 